首页 > 试题广场 >

翻转单词

[编程题]翻转单词
  • 热度指数:1357 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个字符串,请你把字符串中所有用空格隔开的单词翻转,保留原有的空格和单词顺序

数据范围:字符串长度满足 , 字符串中仅包含小写英文字母和空格
示例1

输入

"I am a programmer"

输出

"I ma a remmargorp"
示例2

输入

"nowcoder"

输出

"redocwon"
class Solution:
    def reverseWord(self , str: str) -> str:
        # write code here
        return ' '.join(list(map(lambda x:x[::-1], str.split(' '))))

发表于 2022-07-09 00:00:36 回复(0)
class Solution:
    def reverseWord(self , str: str) -> str:
        # write code here
        l = str.split(' ')
        return ' '.join([w[::-1] for w in l])

发表于 2022-04-22 16:26:31 回复(0)